﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class EditCustCreditQuery : BasePage
    {
        private ICustCreditBLL creditBLL = IocContainer.Instance.Resolve<ICustCreditBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!IsPostBack)
            {
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int creditId = 0;
                    int.TryParse(Request.QueryString["CreditId"], out creditId);
                    if (creditId > 0)
                    {
                        DataTable dt = creditBLL.Query(string.Format(" and FId = {0}", creditId));
                        if (dt.Rows.Count > 0)
                        {
                            DataRow dr = dt.Rows[0];
                            this.txtCustName.Text = dr["FCustName"].ToString();// model.FCustId.ToString();
                            this.hidCustId.Value = dr["FCustId"].ToString();
                            base.BinDropDownList<DropDownList>(ddlstCreditType, dr["FCreditType"].ToString(), true);
                            base.BinDropDownList<DropDownList>(ddlstType, dr["FType"].ToString(), true);
                            base.BinDropDownList<DropDownList>(ddlstCreditWay, dr["FCreditWay"].ToString(), true);
                            txtAmount.Text = dr["FAmount"].ToString();//model.FAmount.ToString();
                            txtStartDate.Text = dr["FBeginDate"].ToString(); // model.FBeginDate == null ? "" : Convert.ToDateTime(model.FBeginDate).ToString("yyyy-MM-dd");
                            txtEndDate.Text = dr["FEndDate"].ToString(); // model.FEndDate == null ? "" : Convert.ToDateTime(model.FEndDate).ToString("yyyy-MM-dd");
                            txtYearAmount.Text = dr["FYearAmount"].ToString(); //model.FYearAmount.ToString();
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                CustCredit model = new CustCredit();
                model.FCustId = Int32.Parse(this.hidCustId.Value.Trim());
                model.FCreditType = Int32.Parse(ddlstCreditType.SelectedValue);
                model.FType = Int32.Parse(ddlstType.SelectedValue);
                model.FCreditWay = Int32.Parse(ddlstCreditWay.SelectedValue);
                model.FAmount = decimal.Parse(txtAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txtStartDate.Text.Trim()))
                    model.FBeginDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                if (!string.IsNullOrEmpty(txtEndDate.Text.Trim()))
                    model.FEndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                if (!string.IsNullOrEmpty(txtYearAmount.Text.Trim()))
                    model.FYearAmount = decimal.Parse(txtYearAmount.Text.Trim());
                model.FisYes = false;
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    creditBLL.Add(model);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int creditId = 0;
                    int.TryParse(Request.QueryString["CreditId"], out creditId);
                    model.FId = creditId;
                    creditBLL.Update(model);
                    isOK = true;
                }
                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}